<% markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, extensions = {}) %>
<% colors = {"critical"=>"darkred", "high"=> "red", "medium"=> "orange", "low"=> "yellow", "informational"=> "blue", "observation"=> "info"} %>
<% index = SecureRandom.hex %>
<% vuln_index = SecureRandom.hex %>
<!-- move to application.js -->

<dl class="accordion updateable" data-updateable-id="<%= finding["id"] %>" data-accordion>
  <dd class="accordion-navigation">
    <a href="#accordion_<%= index %>">
      <button href="#" id="drop_<%= index %>1_button" data-dropdown="drop_<%= index %>1" aria-controls="drop_<%= index %>1" aria-expanded="false" class="severity label tiny button dropdown adjust_top <%= colors[finding["severity"].to_s.downcase].to_s %>"><%= finding["severity"].to_s.titleize %></button>
      <strong><%= finding["name"] || finding["type"] || finding["details"]  %></strong>
      <button href="#" id="drop_<%= index %>_button" data-dropdown="drop_<%= index %>" aria-controls="drop_<%= index %>" aria-expanded="false" class="status right tiny button dropdown"><%= finding["status"] %></button>
      <button href="#" id="drop_<%= index %>2_button" data-dropdown="drop_2<%= index %>" aria-controls="drop_2<%= index %>" aria-expanded="false" class="status right tiny button dropdown">Actions</button><br>
      <ul id="drop_<%= index %>1" data-dropdown-content class="f-dropdown" aria-hidden="true">
        <% ["Critical", "High", "Medium", "Low", "Observation", "Informational"].each do |status| %>
          <li>
            <%= link_to status, update_metadata_result_path(id: @result, "key[0]"=> "vulnerabilities.[id:#{finding["id"]}].severity", "value[0]"=> status, "target[0]"=>"#drop_#{index}1_button"), remote: true %>
          </li>
        <% end %>
      </ul>
      <ul id="drop_<%= index %>" data-dropdown-content class="f-dropdown" aria-hidden="true">
        <% if Vulnerability.open_statuses.include?(finding.try(:[],"status").to_s.downcase) %>
          <% ["Remediated", "False Positive", "Ignored"].each do |status| %>
            <li>
              <%= link_to status, update_metadata_result_path(id: @result, "key[0]"=> "vulnerabilities.[id:#{finding["id"]}].status", "value[0]"=> status, "target[0]"=>"#drop_#{index}_button"), remote: true %>
            </li>
          <% end %>
        <% else %>
          <% ["Reopened"].each do |status| %>
            <li>
              <%= link_to status, update_metadata_result_path(id: @result, "key[0]"=> "vulnerabilities.[id:#{finding["id"]}].status", "value[0]"=> status, "target[0]"=>"#drop_#{index}_button"), remote: true %>
            </li>
          <% end %>
        <% end %>
      </ul>
      <ul id="drop_2<%= index %>" data-dropdown-content class="f-dropdown" aria-hidden="true">
        <% if move_vulnerability_task.present? %>
        <li>

          <% form = form_tag run_task_path(id: move_vulnerability_task), remote: true do |f| %> 
            <%= "<div id=\"task_type_options\"></div>".html_safe %>
            <%= submit_tag "Submit",  :class=>"button tiny close-modal" %>
            <%= button_tag "Cancel", :type=>:button, :class=>"button secondary tiny close-modal" %>
          <% end %>

          <%= link_to "Move","#", class: "remote-form", data: {form: form.gsub("\n", ""), "form-path": on_demand_options_task_path(id: move_vulnerability_task, options:{old_result_id: @result.id, vulnerability_ids: finding["id"]}, hidden_fields:["old_result_id", "vulnerability_ids"])} %>

        </li>
        <% end %>
        <% if create_vulnerability_jira_task.present? %>
        <li>

          <% form = form_tag run_task_path(id: create_vulnerability_jira_task), remote: true do |f| %> 
            <%= "<div id=\"task_type_options\"></div>".html_safe %>
            <%= submit_tag "Submit",  :class=>"button tiny close-modal-on-submit" %>
            <%= button_tag "Cancel", :type=>:button, :class=>"button secondary tiny close-modal" %>
          <% end %>

          <%= link_to "Create Jira","#", class: "remote-form", data: {"form": form.gsub("\n", ""), "form-path": on_demand_options_task_path(id: create_vulnerability_jira_task, options:{result_id: @result.id, vulnerability_id: finding["id"], current_user_id: current_user.id}, hidden_fields:["result_id", "vulnerability_id", "current_user_id"])} %>
          

        </li>
        <% end %>
      </ul>
    </a>
    <div id="accordion_<%= index %>" class="content panel">
      <div class="row">
        <div class="large-8 columns">
          <dl class="dl-horizontal" style="word-wrap: break-word">
            <% if finding["type"].present? %>
              <dt>Type</dt>
              <dd><%= finding["type"] %></dd>
            <% end %>
            <dt>URL</dt>
            <dd><a href="<%= finding["url"] %>"><%= finding["url"] %></a></dd>
            <% if finding["commit_branch"].present? %>
              <dt>Commit Branch</dt>
              <dd><%= finding["commit_branch"] %></dd>
            <% end %>
            <% if finding["commit_name"].present? %>
              <dt>Commit Name</dt>
              <dd><%= finding["commit_name"] %></dd>
            <% end %>
            <% if finding["commit_email"].present? %>
              <dt>Commit Email</dt>
              <dd><%= finding["commit_email"] %></dd>
            <% end %>
            <% if finding["match_count"].present? %>
              <dt>Match Count</dt>
              <dd><%= finding["match_count"].to_s %></dd>
            <% end %>
            <% if finding["reporter"].try(:[],"username").present? %>
              <dt>Reporter</dt>
              <dd><%= finding["reporter"]["username"] %></dd>
            <% end %>
            <% if finding["reward"] %>
              <dt>Reward</dt>
              <dd><%= number_to_currency finding["reward"] %></dd>
            <% end %>
            
          </dl>
        </div>
        <div class="large-4 columns">
          <dl class="dl-horizontal">
            <dt>Identified</dt>
            <dd><%= time_ago_in_words(finding["identified"]) %> ago</dd>
            <dt>Source(s)</dt>
            <dd><%= finding["source"].join(", ") %></dd>
            <% if finding["external_link"] %>
            <dt>External Link(s)</dt>
            <dd><%= finding["external_link"].map{|k,v| link_to(h(k), h(v)) }.join(", ").html_safe %></dd>
            <% end %>

            <% vuln_index = SecureRandom.hex %>
            <dt>Jira ID(s)</dt>
            <dd id="jira_ids_<%= vuln_index %>" name="jira_ids_<%= vuln_index %>">

            <% if finding.try(:[], "jira_ids").to_s != "" %>
            <% finding.try(:[], "jira_ids").split(',').each do |jira| %>
              <% if Rails.configuration.try(:jira_url) %>
                <a href="<%=Rails.configuration.try(:jira_url)%>/browse/<%= jira %>" target="_blank"><%= jira %></a>
              <% else %>
                <%= jira %>
              <% end %>
            
            <% end %>
            <% end %>
            </dd>
            <dt>Update Jira</dt>
            <dd>
              <%= form_tag update_metadata_result_path(id: @result, "key[0]"=> "vulnerabilities.[id:#{finding["id"]}].jira_ids", "target[0]" => "#jira_ids_" + vuln_index), remote: true do %>
                <div class="row collapse">
                  <div class="small-8 columns">
                    <%= text_field_tag "value[0]", nil, placeholder: "Jira IDs (comma separated)", id: "vulnerability_jira_field_" + vuln_index %>
                  </div>
                  
                  <div class="small-2 columns">
                    <%= submit_tag "Update", class: "button postfix tiny update_jira", remote: true, :data => {result_id: vuln_index} %>
                  </div>
                </div>
              <% end %>
          </dd>
        </dl>
      </div>
    </div>

    <% if finding["source_code_file"].to_s.strip != "" %>
      <div class="row">
        <div class="large-8 columns">
          <dl class="dl-horizontal">
            <dt>Source Code File</dt>
            <dd><%= finding["source_code_file"] %></dd>
          </dl>
        </div>
        <div class="large-4 columns">
          <dl class="dl-horizontal">
            <dt>Line</dt>
            <dd><%= finding["source_code_line"] %></dd>
          </dl>
        </div>
      </div>
    <% end %>
    <% if finding.try(:[], "source").include? "github"%>
      <div class="row">
        <div class="large-12 columns">
          <% if finding.try(:[], "match_location") == "content" %>
            <pre>Confidence: <%= "#{finding.try(:[], "score")}" %></pre>
            <dl class="dl-horizontal">
              <div class="panel" style="word-wrap: break-word; font-family: monospace;"">
                <%= highlight(h(finding["code_fragment"]), h(finding["term"])) %>
              </div>
            </dl>
          <% else %>
            <dl class="dl-horizontal">
              <div class="panel" style="word-wrap: break-word; font-family: monospace;"">
                <%= highlight(h(finding["url"]), h(finding["term"])) %>
              </div>
            </dl>
          <% end %>
        </div>
      </div>
    <% end %>

    <% if finding.try(:[], "source").include? "github_event"%>

  <div class="row">
        <div class="large-8 columns">
          <dl class="dl-horizontal">
          <% if finding.try(:[], "regex").present? %>
            <dt>Content</dt>
            <dd><%= "<span data-tooltip aria-haspopup=\"true\" class=\"has-tip\" title=\"#{finding['regex']}\">".html_safe %>
            <%= "#{finding["details"]}" %>
            </span>
            </dd>
            <% else %>
            <dt>Content</dt>
            <dd><%= "#{finding["details"]}" %></dd>
            <% end %>
          </dl>
        </div>
      </div>
      <dl class="dl-horizontal">
        <div class="panel" style="word-wrap: break-word; font-family: monospace;">
          <% if finding["line_number"].present? and finding["line_number"].to_i == 0 %>

          <% else %>
            <% (finding["before"]||[]).each do |i| %>
            </br>
            <%= i[0].to_s.strip %> <%= i[1].to_s.rstrip %>
          <% end %>
        <% end %>
      </br>

      <% matcher = Regexp.new finding["regex"] %>
      <%= finding["line_number"].to_s.strip %> <b><%= highlight(finding["code_fragment"].to_s,matcher) %></b>

      <% (finding["after"]||[]).each do |i| %>
      </br>
      <%= i[0].to_s.strip %> <%= i[1].to_s.rstrip %>
    <% end %>
  </div>
</dl>
    <% end %>

    <% unless (["curl", "sketchy", "depot"] && finding.try(:[], "source")).empty? %>
      <div class="row">
        <div class="large-12 columns">
          <% if finding.try(:[], "match_location") == "headers" and finding.try(:[], "code_fragment") != nil%>
            <div class="row">
              <div class="large-8 columns">
                <dl class="dl-horizontal">
                  <dt>Header Match</dt>
                  <dd><%= "#{finding["term"]}" %></dd>
                </dl>
              </div>
            </div>
            <dl class="dl-horizontal">
              <div class="panel" style="word-wrap: break-word; font-family: monospace;"">
                <% if finding["line_number"].present? and finding["line_number"].to_i == 0 %>
                  <b><%= highlight(h(finding["code_fragment"]), h(finding["regex"])) %></b>
                <% else %>
                  <% (finding["before"]||[]).each do |i| %>
                  </br>
                  <%= i[1].to_s.rstrip %>
                <% end %>
              <% end %>
            </br>
            <b><%= highlight(h(finding["code_fragment"]), h(finding["regex"])) %></b>
            <% (finding["after"]||[]).each do |i| %>
            </br>
            <%= i[1].to_s.rstrip %>


          <% end %>
        </div>
      </dl>

        <% elsif finding.try(:[], "match_location") == "file" and finding.try(:[], "code_fragment") != nil%>
          <div class="row">
            <div class="large-8 columns">
              <dl class="dl-horizontal">
                <dt>File Match</dt>
                <dd><%= "#{finding["term"]}" %></dd>
              </dl>
            </div>
          </div>
          <dl class="dl-horizontal">
            <div class="panel" style="word-wrap: break-word; font-family: monospace; white-space:pre-wrap;">
          <b><%= highlight(h(simple_format(finding["code_fragment"].to_s.gsub('\n', '<br/>'))), h(finding["term"])) %></b>
      </div>
    </dl>
      <!-- CUSTOMIZE THIS FOR PATHS AND BOTH,
        THINK ABOUT TERMS WE MAY NEED TO -->
    <% elsif finding.try(:[], "match_location") == "both" and finding.try(:[], "code_fragment") != nil%>
      <div class="row">
        <div class="large-8 columns">
          <dl class="dl-horizontal">
            <dt>Content and Path Match</dt>
            <dd><%= "#{finding["term"]}" %></dd>
            <dt>Path</dt>
            <dd><%= "#{finding["path"]}" %></dd>
          </dl>
        </div>
      </div>
      <dl class="dl-horizontal">
        <div class="panel" style="word-wrap: break-word; font-family: monospace;"">
          <% if finding["line_number"].present? and finding["line_number"].to_i == 0 %>
            <b><%= highlight(h(finding["code_fragment"]), h(finding["term"])) %></b>
          <% else %>
            <% (finding["before"]||[]).each do |i| %>
            </br>
            <%= i[0].to_s.strip %> <%= i[1].to_s.rstrip %>
          <% end %>
        <% end %>
      </br>
      <%= finding["line_number"].to_s.strip %> <b><%= highlight(h(finding["code_fragment"].to_s),h(finding["term"])) %></b>
      <% (finding["after"]||[]).each do |i| %>
      </br>
      <%= i[0].to_s.strip %> <%= i[1].to_s.rstrip %>
    <% end %>
  </div>
</dl>
<% elsif finding.try(:[], "match_location") == "path"%>
  <div class="row">
    <div class="large-8 columns">
      <dl class="dl-horizontal">
        <dt>Path Match</dt>
        <dd><%= "#{finding["path"]}" %></dd>
      </dl>
    </div>
  </div>
  <dl class="dl-horizontal">
    <div class="panel" style="word-wrap: break-word; font-family: monospace;"">
      <b><%= highlight(h(finding["url"]), h(finding["path"])) %></b>
    </br>
  </div>
</dl>
<% elsif finding.try(:[], "match_location") == "content" and finding.try(:[], "code_fragment") != nil%>
  <div class="row">
    <div class="large-8 columns">
      <dl class="dl-horizontal">
        <dt>Content Match</dt>
        <dd><%= "#{finding["term"]}" %></dd>
        <% unless finding["status_code"].nil? %>
          <dt>Status Code</dt>
          <dd><%= "#{finding["status_code"]}" %></dd>
        <% end %>
      </dl>
    </div>
  </div>
  <dl class="dl-horizontal">
    <div class="panel" style="word-wrap: break-word; font-family: monospace;"">
      <% if finding["line_number"].present? and finding["line_number"].to_i == 0 %>
        <b><%= highlight(h(finding["code_fragment"]), h(finding["term"])) %></b>
      <% else %>
        <% (finding["before"]||[]).each do |i| %>
        </br>
        <%= i[0].to_s.strip %> <%= i[1].to_s.rstrip %>
      <% end %>
    <% end %>
  </br>
  <%= finding["line_number"].to_s.strip %> <b><%= highlight(h(finding["code_fragment"].to_s),h(finding["term"])) %></b>
  <% (finding["after"]||[]).each do |i| %>
  </br>
  <%= i[0].to_s.strip %> <%= i[1].to_s.rstrip %>
<% end %>
</div>
</dl>
<% else %>
  <div class="row">
    <div class="large-8 columns">
      <dl class="dl-horizontal">
        <dt>Status Code</dt>
        <dd><%= "#{finding["status_code"]}" %></dd>
      </dl>
    </div>
  </div>
  <!-- Ends inside if -->
<% end %>
<!-- Ends unless clause -->
</div>
</div>
<% end %>
<% if !finding["source_code"].nil? && !finding["source_code"]["hit_source_line"].nil? %>
  <div class="row">
    <div class="large-12 columns">
      <dl class="dl-horizontal">
        <dt>Source Code</dt>
        <% if !finding["source_code"]["before"].nil? %>
          <% finding["source_code"]["before"].each do |key, val| %>
            <dd><%= key.to_i %>: <%= h(val).gsub(" ", "&nbsp;").gsub("\t", "&nbsp;&nbsp;").html_safe %></dd>
          <% end %>
        <% end %>
        <dd><b><%= finding["source_code"]["hit_line_number"].to_i %>: <%=  h(finding["source_code"]["hit_source_line"]).gsub(" ", "&nbsp;").gsub("\t", "&nbsp;&nbsp;").html_safe %></b></dd>
        <% if !finding["source_code"]["after"].nil? %>
          <% finding["source_code"]["after"].each do |key, val| %>
            <dd><%= key %>: <%= h(val).gsub(" ", "&nbsp;").gsub("\t", "&nbsp;&nbsp;").html_safe %></dd>
          <% end %>
        <% end %>
      </dl>
    </div>
  </div>
<% end %>
<% if finding["details"].to_s.strip != "" and finding["source"].exclude? "github_event" %>
  <div class="row">
    <div class="large-12 columns">
      <dl class="dl-horizontal">
        <dt>Details</dt>
        <dd><%= markdown.render(finding["details"].to_s).html_safe %></dd>
      </dl>
    </div>
  </div>
<% end %>
<% if finding["confidence_level"].to_s.strip != "" %>
  <div class="row">
    <div class="large-8 columns">
      <dl class="dl-horizontal">
        <dt>Confidence Level</dt>
        <dd><%= finding["confidence_level"] %></dd>
      </dl>
    </div>
    <div class="large-4 columns">
      <dl class="dl-horizontal">
        <dt></dt>
        <dd></dd>
      </dl>
    </div>
  </div>
<% end %>
<% if !finding["attack_vectors"].blank? %>
  <h3 class="section_header">Attack Vectors</h3>
  <table>
    <thead>
      <tr>
        <th>Input Type</th>
        <th>Input Name</th>
        <th>Payload</th>
        <th>Method</th>
        <th></th>
      </tr>
    </thead>
    <tbody>
      <% finding["attack_vectors"].each do |vect| %>
        <tr>
          <td><%= vect["type"] %></td>
          <td><%= vect["name"] %></td>
          <td><%= vect["payload"] %></td>
          <td><%= vect["method"] %></td>
          <td>
            <% if vect["note"].present? %>
              <span data-tooltip aria-haspopup="true" class="has-tip tip-left" title="<%= h(vect["note"])%>"><i class="fi-info"></i></span></td>
          <% end %>
        </tr>
      <% end %>
    </tbody>
  </table>
<% end %>
</div>
</dd>
</dl>
